/*
 * Copyright 2007-2009 Georgina Stegmayer, Milagros Gutiérrez, Jorge Roa
 * y Milton Pividori.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ar.edu.utn.frsf.cidisi.upcolbpip.upgen.util;

import java.util.Vector;

/**
 * @author Jorge M. Roa
 * @version 1.0
 * @created 08-Mar-2007 13:16:04
 */
public class NTree implements Cloneable, Comparable<NTree> {

    protected int deep;
    protected double cost;

    protected NTree parent;
    protected Vector<NTree> sons;
    protected int executionOrder;
    protected String elementName;
    
    public NTree() {
        this.deep = 0;
        this.parent = null;
        this.sons = new Vector<NTree>();
        this.executionOrder = 0;
        this.elementName = "Root";
    }

    public NTree(NTree firstNode,  int order) {
        this.deep = firstNode.getDeep() + 1;
        this.parent = firstNode;
        this.sons = new Vector<NTree>();
        this.executionOrder = order;
        this.elementName = elementName;
    }

    public String getElementName() {
    	return elementName;
    }
    
    public void setElementName(String elementName){
    	
    	this.elementName = elementName ;
    	
    }
    
    /**
     * 
     * @param son
     */
    public void addSon(NTree son) {
        this.getSons().addElement(son);
    }

    public Object clone() {

        NTree node = new NTree();

        /*		if (parent!=null)
        node.setParent((NTree)parent.clone());*/
        node.setParent(parent);
        node.setExecutionOrder(executionOrder);

        // TODO: Ac� hay que clonar a los hijos tambi�n!!???.-

        /*		node.setSons((Vector<NTree>)sons.clone());*/
        node.setSons(sons);

        return node;
    }

    public void setExecutionOrder(int executionOrder) {
		this.executionOrder = executionOrder;
	}


    public double getCost() {
        return cost;
    }

    public int getDeep() {
        return deep;
    }

    public NTree getParent() {
        return parent;
    }

    public Vector<NTree> getSons() {
        return sons;
    }

    public Vector<NTree> getSonsTotal() {
        Vector<NTree> temp = new Vector<NTree>();

        // Agrego primero mis hijos
        temp.addAll(this.sons);

        // Agrego después los hijos de mis hijos recursivamente
        for (int i = 0; i < this.getSons().size(); i++) {
            temp.addAll(this.getSons().elementAt(i).getSonsTotal());
        }
        return temp;
    }


    /**
     * 
     * @param cost
     */
    public void setCost(double cost) {
        this.cost = cost;
    }

    /**
     * 
     * @param deep
     */
    public void setDeep(int deep) {
        this.deep = deep;
    }

    /**
     * 
     * @param father
     */
    public void setParent(NTree parent) {
        this.parent = parent;
    }

    /**
     * 
     * @param sons
     */
    public void setSons(Vector<NTree> sons) {
        this.sons = sons;
    }

    /**
     * 
     * @param state
     */

    public String toString() {
        String eo = "Id=\"" + executionOrder + "\" ";
      
        return eo ;
    }

  
    public int getExecutionOrder() {
		return executionOrder;
	}

	
    @Override
    public boolean equals(Object obj) {
    //    return agentState.equals(((NTree) obj).getAgentState());
    	return true;
    }

    @Override
    public int compareTo(NTree o) {
        if (this.cost == o.cost) {
        	// If both nodes have the same cost then it is necessary to check the order
        	// in which those nodes have been created.-
        	if (this.executionOrder == o.executionOrder) {
        		return 0;
            } else if (this.executionOrder < o.executionOrder) {
                return -1;
            } else {
                return 1;
            }
        } else if (this.cost < o.cost) {
            return -1;
        } else {
            return 1;
        }
    }
}
